<template>
  <div class="privilege">
    <!--查询表单-->
    <el-form :inline="true" class="demo-form-inline">
      <el-button type="primary" @click="toAdd">添加</el-button>
      <el-form-item>
        <el-input placeholder="权限名称" />
      </el-form-item>
      <el-form-item label="添加时间">
        <el-date-picker
          type="datetime"
          placeholder="选择开始时间"
          value-format="yyyy-MM-dd HH:mm:ss"
          default-time="00:00:00"
        />
      </el-form-item>
      <el-form-item>
        <el-date-picker
          type="datetime"
          placeholder="选择截止时间"
          value-format="yyyy-MM-dd HH:mm:ss"
          default-time="00:00:00"
        />
      </el-form-item>
      <el-button type="primary" icon="el-icon-search" @click="getList()">查询</el-button>
      <el-button type="default" @click="resetData()">清空</el-button>
    </el-form>
    <!-- 表格 -->
    <el-table :data="privileges" row-key="id" size="small" :tree-props="{children: 'children'}">
      <el-table-column label="编号" type="index" :index="1" align="center"></el-table-column>
      <el-table-column prop="name" label="权限名称" align="center"></el-table-column>
      <el-table-column prop="path" label="权限路径" align="center"></el-table-column>
      <el-table-column fixed="right" label="操作" align="center">
        <template slot-scope="scope">
          <el-button type="info" @click="drawer = true" size="mini">详情</el-button>
          <el-button type="primary" @click="edit(scope.row)" size="mini">编辑</el-button>
          <el-button type="danger" @click="del(scope.row.id)" size="mini">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 表格结束 -->
    <!-- 模态框 -->
    <el-dialog title="权限信息" :visible.sync="visible">
      <el-form :model="form" :rules="rules" ref="ruleForm" label-width="80px">
        <el-form-item label="权限名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入权限名称"></el-input>
        </el-form-item>
        <el-form-item label="权限路径" prop="path">
          <el-input v-model="form.path" placeholder="请输入权限路径"></el-input>
        </el-form-item>
        <el-form-item label="所属权限" prop="parentId">
          <el-select v-model="form.parentId" placeholder="请选择所属权限">
            <el-option v-for="c in privileges" :key="c.id" :label="c.name" :value="c.id"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button size="small" @click="close('ruleForm')">取消</el-button>
        <el-button size="small" type="primary" @click="submit('ruleForm')">确定</el-button>
      </div>
    </el-dialog>
    <!-- 模态框结束 -->
  </div>
</template>
<script>
import { get, post } from '@/utils/request'
export default {
  data() {
    return {
      name: '权限管理',
      visible: false,
      form: {},
      privileges: [],
      ruleForm: {
        name: '',
        path: ''
      },
      rules: {
        name: [{ required: true, message: '请输入权限名称', trigger: 'blur' }],
        path: [{ required: true, message: '请输入权限路径', trigger: 'blur' }]
      }
    }
  },
  created() {
    this.loadprivileges()
  },
  methods: {
    // 加载权限信息
    loadprivileges() {
      let url = 'http://47.93.12.6:8889/privilege/selectAllWithChild'
      get(url).then(response => {
        this.privileges = response.data
      })
    },
    toAdd() {
      this.form = {}
      this.visible = true
    },
    del(id) {
      this.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        // 删除
        let url = 'http://47.93.12.6:8889/privilege/deleteById'
        get(url, { id: id }).then(response => {
          // 提示
          this.$message({ type: 'success', message: response.message })
          // 刷新数据
          this.loadprivileges()
        })
      })
    },
    edit(row) {
      this.visible = true
      this.form = row
    },
    close(form) {
      //取消按钮实现
      this.visible = false
      this.$refs[form].resetFields()
    },
    submit(form) {
      this.$refs[form].validate(valid => {
        if (valid) {
          // 表单验证成功的情况
          let url = 'http://47.93.12.6:8889/privilege/saveOrUpdate'
          post(url, this.form).then(response => {
            this.$message({ type: 'success', message: response.message })
            this.visible = false
            this.loadprivileges()
          })
        } else {
          return false
        }
      })
    }
  }
}
</script>
<style scoped>
.btns {
  margin-bottom: 0.5em;
}
</style>